package com.cj.mapper;

import com.cj.pojo.User;
import com.cj.pojo.UserStatus;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;

@Mapper
public interface UserMapper {
    //根据用户名查询用户
    @Select("select * from user where username=#{username}")
    User findByUserName(String username);

    //添加
    @Insert("insert into user(username,password,create_time,update_time)" +
        " values(#{username},#{password},now(),now())")
    int add(String username, String password);

    @Update("update user set nickname=#{nickname},email=#{email},update_time=#{updateTime} where id=#{id}")
    void update(User user);

    @Update("update user set user_pic=#{avatarUrl},update_time=now() where id=#{id}")
    void updateAvatar(String avatarUrl,Integer id);

    @Update("update user set password=#{md5String},update_time=now() where id=#{id}")
    void updatePwd(String md5String, Integer id);

    List<User> listUsers(UserStatus status, String role);

    @Update("update user set status=#{status},update_time=now() where id=#{id}")
    void updateStatus(Long id, UserStatus status);

    @Update("update user set role=#{role},update_time=now() where id=#{id}")
    void updateRole(Long id, String role);

    @Select("select * from user where id=#{id}")
    User findById(Long id);

    @Select("select count(*) from article where create_user=#{userId} and is_deleted=0")
    int countArticlesByUserId(Long userId);

    @Select("select count(*) from category where create_user=#{userId} and is_deleted=0")
    int countCategoriesByUserId(Long userId);

    @Select("select count(*) from comment where user_id=#{userId} and is_deleted=0")
    int countCommentsByUserId(Long userId);

    @Update("update user set is_deleted=1,update_time=now() where id=#{id}")
    int deleteById(Long id);
}
